const wechat_open = import.meta.env.VITE_WX_OPEN;
const wechat = import.meta.env.VITE_WX_URL;
import {
	storeToRefs
} from 'pinia';
import {
	loginWechat
} from "@/request/api";
import useStore from "@/stores/index.js";

export default function useBase() {
	const {
		useLoginStore
	} = useStore();
	const {
		code,
		token
	} = storeToRefs(useLoginStore);
	const wxLogin = async (params) => {

		// return new Promise(async (resolve, reject) => {
		// 	if (!token.value && !code.value) {
		// 		let wechat_url = params ? `${wechat}${params}` : wechat;
		// 		if (wechat_open == 'True') {
		// 			window.top.location.href = wechat_url;
		// 		}
		// 		reject();
		// 		return false;
		// 	}
		// 	if (!token.value) {
		// 		let invitation_code = uni.getStorageSync('invitation');
		// 		let {
		// 			token
		// 		} = await loginWechat({
		// 			code: code.value,
		// 			invitation_code
		// 		});
		// 		useLoginStore.changeToken(token);
		// 		resolve();
		// 	}
		// });
		if (!token.value && !code.value) {
			let wechat_url = params ? `${wechat}${params}` : wechat;
			if (wechat_open == 'True') {
				window.top.location.href = wechat_url;
			}
			return false;
		}
		if (!token.value) {
			let invitation_code = uni.getStorageSync('invitation');
			let {
				token
			} = await loginWechat({
				code: code.value,
				invitation_code
			});
			useLoginStore.changeToken(token);
		}
	};
	return {
		wxLogin
	}
}